
package com.insigma.business.qrcode.common.service.impl;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.insigma.business.qrcode.common.dao.*;
import com.insigma.business.qrcode.common.dto.CommonAdminAreaBSearcher;
import com.insigma.business.qrcode.common.dto.GovEmployeeJobBSearcher;
import com.insigma.business.qrcode.common.dto.GovEmployeeJobDO;
import com.insigma.business.qrcode.common.dto.req.QueryAreaStreetReqDTO;
import com.insigma.business.qrcode.common.service.CommonAdminAreaService;
import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.soldier.dao.GovEmployeeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.insigma.business.qrcode.soldier.entity.GovOrgDO;

import java.util.*;


@Service("commonAdminAreaService")
public class CommonAdminAreaServiceImpl implements CommonAdminAreaService {

    @Autowired(required = false)
    private CommonAdminAreaDao commonAdminAreaDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    @Autowired(required = false)
    private GovEmployeeDao govEmployeeDao;

    @Autowired(required = false)
    private GovEmployeeJobDao govEmployeeJobDao;

    @Autowired(required = false)
    private GovOrgDao govOrgDao;

    
    @Override
    public List<CommonAdminAreaDO> search(final CommonAdminAreaBSearcher commonAdminAreaBSearcher) {
        return commonAdminAreaDao.search(commonAdminAreaBSearcher);
    }

    @Override
    public CommonAdminAreaDO get(final String id){
        return commonAdminAreaDao.get(id);
    }

    @Override
    public CommonAdminAreaDO getQg(String id) {
        return commonAdminAreaDao.getQg(id);
    }

    @Override
    public String getQgFullValuePath(String id) {
        int level = AdminAreaCodeUtils.getLevel(id);
        List<String> list = Lists.newArrayList();
        for (int i = 1; i <=level; i++) {
            String s = AdminAreaCodeUtils.cutAreaCode(id, String.valueOf(i));
            CommonAdminAreaDO qg = getQg(AdminAreaCodeUtils.fillAreaCode(s));
            list.add(qg.getValue());
        }
        return "/" + String.join("/",list);
    }

    
    @Override
    public Integer insert(final CommonAdminAreaDO commonAdminAreaDO){

        return commonAdminAreaDao.insert(commonAdminAreaDO);
    }

    
    @Override
    public Integer update(final CommonAdminAreaDO commonAdminAreaDO){
        return commonAdminAreaDao.update(commonAdminAreaDO);
    }


    @Override
    public Integer delete(final String id, final String modifierType, final Long modifierId){
        return commonAdminAreaDao.delete(id,modifierType,modifierId);
    }

    @Override
    public void testConvertToArea() {
        List<CommonAdminAreaDO> result = Lists.newArrayList();
        CommonAdminAreaDO pr = new CommonAdminAreaDO();
        pr.setId("330000000000");
        pr.setValue("浙江省");
        pr.setVersion(1);
        pr.setLevel(1);
        pr.setFullIdPath("330000000000");
        pr.setFullValuePath("浙江省");
        pr.setDeleted(false);
        pr.setCreatorId(1L);
        pr.setCreateDatetime(new Date());
        result.add(pr);

        List<Map> cityResult = commonAdminAreaDao.selectCity();
        Map<Object, Object> cityMap = Maps.newHashMap();
        for (Map map : cityResult) {
            cityMap.put(map.get("statistical_code"), map.get("name"));

            CommonAdminAreaDO c = new CommonAdminAreaDO();
            c.setId((String) map.get("statistical_code"));
            c.setValue((String)map.get("name"));
            c.setVersion(1);
            c.setLevel(2);
            c.setParentId((String) map.get("province_statistical_code"));
            c.setFullIdPath((String) map.get("statistical_code"));
            c.setFullValuePath("浙江省" + "/" + (String) map.get("name"));
            c.setDeleted(false);
            c.setCreatorId(1L);
            c.setCreateDatetime(new Date());
            result.add(c);
        }

        List<Map> countyResult = commonAdminAreaDao.selectCounty();
        Map<Object, Object> countyMap = Maps.newHashMap();
        for (Map map : countyResult) {
            countyMap.put(map.get("statistical_code"), map.get("name"));

            CommonAdminAreaDO c = new CommonAdminAreaDO();
            c.setId((String) map.get("statistical_code"));
            c.setValue((String)map.get("name"));
            c.setVersion(1);
            c.setLevel(3);
            c.setParentId((String) map.get("city_statistical_code"));
            c.setFullIdPath((String) map.get("statistical_code"));
            c.setFullValuePath("浙江省"+ "/"+ getCityValue(cityMap, (String) map.get("statistical_code")) + "/" + (String) map.get("name"));
            c.setDeleted(false);
            c.setCreatorId(1L);
            c.setCreateDatetime(new Date());
            result.add(c);

        }

        List<Map> townResult = commonAdminAreaDao.selectTown();
        Map<Object, Object> townMap = Maps.newHashMap();
        for (Map map : townResult) {
            townMap.put(map.get("statistical_code"), map.get("name"));

            CommonAdminAreaDO t = new CommonAdminAreaDO();
            t.setId((String) map.get("statistical_code"));
            t.setValue((String)map.get("name"));
            t.setVersion(1);
            t.setLevel(4);
            t.setParentId((String) map.get("county_statistical_code"));
            t.setFullIdPath((String) map.get("statistical_code"));
            t.setFullValuePath("浙江省"
                    + "/"+ getCityValue(cityMap, (String) map.get("statistical_code"))
                    + "/" + getCountyValue(countyMap, (String) map.get("statistical_code"))

                    + "/" + (String) map.get("name"));
            t.setDeleted(false);
            t.setCreatorId(1L);
            t.setCreateDatetime(new Date());
            result.add(t);

        }

        List<Map> villageResult = commonAdminAreaDao.selectVillage();
        Map<Object, Object> villageMap = Maps.newHashMap();
        for (Map map : villageResult) {
            villageMap.put(map.get("statistical_code"), map.get("name"));

            CommonAdminAreaDO v = new CommonAdminAreaDO();
            v.setId((String) map.get("statistical_code"));
            v.setValue((String)map.get("name"));
            v.setVersion(1);
            v.setLevel(5);
            v.setParentId((String) map.get("town_statistical_code"));
            v.setFullIdPath((String) map.get("statistical_code"));
            v.setFullValuePath("浙江省"
                    + "/"+ getCityValue(cityMap, (String) map.get("statistical_code"))
                    + "/" + getCountyValue(countyMap, (String) map.get("statistical_code"))
                    + "/" + getTownValue(townMap, (String) map.get("statistical_code"))
                    + "/" + (String) map.get("name"));
            v.setDeleted(false);
            v.setCreatorId(1L);
            v.setCreateDatetime(new Date());
            result.add(v);

        }
        System.out.println("");
        Collections.sort(result, new Comparator<CommonAdminAreaDO>() {
            @Override
            public int compare(CommonAdminAreaDO o1, CommonAdminAreaDO o2) {
                return o1.getId().compareTo(o2.getId());
            }
        });

        for (CommonAdminAreaDO commonAdminAreaDO : result) {
            commonAdminAreaDao.insert(commonAdminAreaDO);
        }
        return;
    }

    @Override
    public List<CommonAdminAreaDO> findSubAreaList(String adminAreaCode) {
        adminAreaCode = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode);
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode) + 1;
        List<CommonAdminAreaDO> result =  commonAdminAreaDao.findSubAreaList(adminAreaCode + "%", level);
        return result;
    }

    @Override
    public CommonAdminAreaDO findAdminAreaOfEmployee(String userId) {
        GovEmployeeJobBSearcher searcher = new GovEmployeeJobBSearcher();
        searcher.setEmployeeId(userId);
        List<GovEmployeeJobDO> search = govEmployeeJobDao.search(searcher);
        String leafInstituteId = search.get(0).getLeafInstituteId();

        GovOrgDO govOrgDO = govOrgDao.get(leafInstituteId);
        String leafAdminAreaId = govOrgDO.getLeafAdminAreaId();
        return commonAdminAreaDao.get(leafAdminAreaId);
    }

    @Override
    public List<CommonAdminAreaDO> getAllSubArea(String areaCode) {
        return commonAdminAreaDao.getAllSubArea(areaCode);
    }

    @Override
    public List<CommonAdminAreaDO> listByCodeAndLevel(String areaCode, int level) {
        return commonAdminAreaDao.listByCodeAndLevel(areaCode, level);
    }

    @Override
    public List<CommonAdminAreaDO> findQGSubAreaList(String cutdAreaCode) {
        if (Strings.isNullOrEmpty(cutdAreaCode)) {
            List<CommonAdminAreaDO> result =  commonAdminAreaDao.findQGSubAreaList(null, 1);
            return result;
        }
        cutdAreaCode = AdminAreaCodeUtils.getCutdAreaCode(cutdAreaCode);
        int level = AdminAreaCodeUtils.getLevel(cutdAreaCode) + 1;
        List<CommonAdminAreaDO> result =  commonAdminAreaDao.findQGSubAreaList(cutdAreaCode + "%", level);
        return result;
    }

    @Override
    public List<CommonAdminAreaDO> getAllSubAreaOfQg(String code) {
        return commonAdminAreaDao.getAllSubAreaOfQg(code);
    }

    @Override
    public List<CommonAdminAreaDO> getAllSubAreaOfQgByValue(String areaName) {
        return commonAdminAreaDao.getAllSubAreaOfQgByValue(areaName);
    }

    @Override
    public List<CommonAdminAreaDO> getSubAreaByValue(String areaName) {
        return commonAdminAreaDao.getSubAreaByValue(areaName);
    }

    @Override
    public Integer updateOfQg(CommonAdminAreaDO commonAdminAreaDO) {
        return commonAdminAreaDao.updateOfQg(commonAdminAreaDO);
    }

    @Override
    public List<CommonAdminAreaDO> getSubAreaByParentIDAndLevel(QueryAreaStreetReqDTO queryAreaStreetReqDTO) {
        return commonAdminAreaDao.getSubAreaByParentIDAndLevel(queryAreaStreetReqDTO);
    }


    private String getCityValue(Map map, String str) {
        String city = str.substring(0,4);
        return (String)map.get(Strings.padEnd(city, 12, '0'));
    }

    private String getCountyValue(Map map, String str) {
        String city = str.substring(0,6);
        return (String)map.get(Strings.padEnd(city, 12, '0'));
    }

    private String getTownValue(Map map, String str) {
        String city = str.substring(0,9);
        return (String)map.get(Strings.padEnd(city, 12, '0'));
    }


}
